package com.a2.appa3.presenter.activity;

import android.content.Intent;
import android.widget.Toast;

import com.a2.appa3.global.HttpAdress;
import com.a2.appa3.global.Info;
import com.a2.appa3.model.CustomObserver;
import com.a2.appa3.model.network.HttpJsonUtils;
import com.a2.appa3.model.network.entity.req.ResCode;
import com.a2.appa3.model.network.entity.req.ResLogin;
import com.a2.appa3.model.network.entity.req.Resregister;
import com.a2.appa3.ui.activity.PasswordActivity;
import com.a2.appa3.ui.activity.RegisterActivity;
import com.a2.appa3.util.SpUtils;
import com.google.gson.Gson;

import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;

/**
 * Created by Administrator on 2019/7/5.
 */
public class RegisterActivityPresenter extends BaseActivityPresenter<RegisterActivity> {
    public RegisterActivityPresenter(RegisterActivity mActivity) {
        super(mActivity);
    }

    /**
     * 验证码
     */
    public void getCode() {
        HttpJsonUtils.getHttpApiInstance()
                .getcode(HttpAdress.getuseAdress() + "api/m/captcha", Info.Token)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new CustomObserver<ResCode>(mActivity) {
                    @Override
                    protected void onSubscribeCustom(Disposable disposable) {

                    }

                    @Override
                    protected void onErrorCustom(Throwable e) {

                    }

                    @Override
                    protected void onCustomNext(ResCode resCode) {
                        if (null != resCode.data)
                            mActivity.onCode(resCode);
                    }
                });
    }

    /**
     * 注册
     * (peuser, password, realname, phone, username, codes, code);
     */
    public void register(String puser, String password, final String realname, String phone, String username, String codes, String code) {
        HttpJsonUtils.getHttpApiInstance()
                .register(HttpAdress.getuseAdress() + "api/m/reg", puser, password, realname, phone, username, codes, code)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new CustomObserver<Resregister>(mActivity) {
                    @Override
                    protected void onSubscribeCustom(Disposable disposable) {
                    }

                    @Override
                    protected void onErrorCustom(Throwable e) {
                        mActivity.onRegister();
                    }

                    @Override
                    protected void onCustomNext(Resregister resregister) {
                        if (resregister.code != 0) {
                            mActivity.onRegister();
                            Toast.makeText(mActivity, resregister.msg, Toast.LENGTH_SHORT).show();
                        } else {
                            mActivity.onUsermessage(resregister);
                        }
                    }
                });
    }

    /**
     * 轮训用户信息
     * (peuser, password, realname, phone, username, codes, code);
     */
    public void finduser(String s) {
        HttpJsonUtils.getHttpApiInstance()
                .init(HttpAdress.getuseAdress() + "api/init", s)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new CustomObserver<ResLogin>() {
                    @Override
                    protected void onSubscribeCustom(Disposable disposable) {

                    }

                    @Override
                    protected void onErrorCustom(Throwable e) {
                        Toast.makeText(mActivity, "查询失败", Toast.LENGTH_SHORT).show();
                    }

                    @Override
                    protected void onCustomNext(ResLogin resLogin) {
                        if (resLogin.code == 0 && resLogin.data != null) {
                            Info.user = resLogin.data;
                            Info.Token = resLogin.data.token;
                            SpUtils.clearUser(mActivity);
                            SpUtils.putString(mActivity, "User", new Gson().toJson(resLogin.data));
                            String user = SpUtils.getString(mActivity, "User");
                            if (new Gson().fromJson(user, ResLogin.DataBean.class) == null) {
                                String user1 = SpUtils.getString(mActivity, "User");
                                if (new Gson().fromJson(user1, ResLogin.DataBean.class) == null) {
                                }
                            }
                            if (resLogin.data.isPhone.equals("1")) {
                            } else {
                                Toast.makeText(mActivity, "请绑定手机号", Toast.LENGTH_SHORT).show();
                            }
                            if (resLogin.data.forcePasswordChange.equals("1")) {
                                Toast.makeText(mActivity, "您的密码不符合规定，请尽快修改", Toast.LENGTH_LONG).show();
                                Intent intent = new Intent(mActivity, PasswordActivity.class);
                                mActivity.startActivity(intent);
                            } else {
                                Toast.makeText(mActivity, "登录成功", Toast.LENGTH_SHORT).show();
                                mActivity.finish();
                            }


                        } else {
                            Toast.makeText(mActivity, "登录失败", Toast.LENGTH_SHORT).show();
                        }
                    }
                });
    }


}

